It shows how to import file(s) to / export file(s) from camera(s), and get file access progress. The major steps include reading a file (such as UserSet1.bin) from camera by calling MV_CC_FileAccessRead(), writing a file (such as UserSet1.bin) to camera by calling MV_CC_FileAccessWrite(), and getting the progress of file access by calling MV_CC_GetFileAccessProgress().
11 currentsystem = platform.system()
12 if currentsystem ==
'Windows':
13 sys.path.append(os.path.join(os.getenv(
'MVCAM_COMMON_RUNENV'),
"Samples",
"Python",
"MvImport"))
15 sys.path.append(os.path.join(
"..",
"..",
"MvImport"))
16 from MvCameraControl_class
import *
19 if sys.version_info[0] < 3:
21 input_func = raw_input
29 Safely decode a string from a ctypes character array. 30 Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments. 32 byte_str = memoryview(ctypes_char_array).tobytes()
35 null_index = byte_str.find(b
'\x00')
37 byte_str = byte_str[:null_index]
40 for encoding
in [
'gbk',
'utf-8',
'latin-1']:
42 return byte_str.decode(encoding)
43 except UnicodeDecodeError:
47 return byte_str.decode(
'latin-1', errors=
'replace')
52 stFileAccessProgress = MV_CC_FILE_ACCESS_PROGRESS()
53 memset(byref(stFileAccessProgress), 0, sizeof(stFileAccessProgress))
56 ret = cam.MV_CC_GetFileAccessProgress(stFileAccessProgress)
57 print (
"State = [%x],Completed = [%d],Total = [%d]" % (ret, stFileAccessProgress.nCompleted, stFileAccessProgress.nTotal))
58 if (ret != MV_OK
or (stFileAccessProgress.nCompleted != 0
and stFileAccessProgress.nCompleted == stFileAccessProgress.nTotal)):
59 print(
'press Enter key to continue.')
64 stFileAccess = MV_CC_FILE_ACCESS()
65 memset(byref(stFileAccess), 0, sizeof(stFileAccess))
66 stFileAccess.pUserFileName =
'UserSet1.bin'.encode(
'ascii')
67 stFileAccess.pDevFileName =
'UserSet1'.encode(
'ascii')
70 ret = cam.MV_CC_FileAccessRead(stFileAccess)
72 print (
"file access read fail ret [0x%x]\n" % ret)
75 ret = cam.MV_CC_FileAccessWrite(stFileAccess)
77 print (
"file access write fail ret [0x%x]\n" % ret)
79 if __name__ ==
"__main__":
83 MvCamera.MV_CC_Initialize()
85 SDKVersion = MvCamera.MV_CC_GetSDKVersion()
86 print (
"SDKVersion[0x%x]" % SDKVersion)
88 deviceList = MV_CC_DEVICE_INFO_LIST()
89 tlayerType = MV_GIGE_DEVICE | MV_USB_DEVICE
92 ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
94 print (
"enum devices fail! ret[0x%x]" % ret)
97 if deviceList.nDeviceNum == 0:
98 print (
"find no Device!")
101 print (
"find %d devices!" % deviceList.nDeviceNum)
103 for i
in range(0, deviceList.nDeviceNum):
104 mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
105 if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE
or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
106 print (
"\ngige device: [%d]" % i)
107 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
108 print (
"device model name: %s" % strModeName)
109 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber)
110 print(
"device serial number: %s" % strSerialNumber)
111 nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
112 nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
113 nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
114 nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
115 print (
"current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
116 elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
117 print (
"\nu3v device: [%d]" % i)
118 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
119 print (
"device model name: %s" % strModeName)
121 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
122 print (
"device serial number: %s" % strSerialNumber)
124 nConnectionNum =
input_func(
"please input the number of the device to connect:")
126 if int(nConnectionNum) >= deviceList.nDeviceNum:
127 print (
"intput error!")
134 stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
136 ret = cam.MV_CC_CreateHandle(stDeviceList)
138 raise Exception (
"create handle fail! ret[0x%x]" % ret)
141 ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
143 raise Exception (
"open device fail! ret[0x%x]" % ret)
146 print (
"read to file.")
147 print(
'press Enter key to start.')
151 hReadThreadHandle = threading.Thread(target=file_access_thread, args=(cam, 1))
152 hReadThreadHandle.start()
154 hProgress1ThreadHandle = threading.Thread(target=progress_thread, args=(cam, 1))
155 hProgress1ThreadHandle.start()
157 raise Exception (
"error: unable to start thread")
162 hReadThreadHandle.join()
163 hProgress1ThreadHandle.join()
166 print (
"write from file.")
167 print(
'press Enter key to start.')
171 hWriteThreadHandle = threading.Thread(target=file_access_thread, args=(cam, 2))
172 hWriteThreadHandle.start()
174 hProgress2ThreadHandle = threading.Thread(target=progress_thread, args=(cam, 2))
175 hProgress2ThreadHandle.start()
177 raise Exception (
"error: unable to start thread")
182 hWriteThreadHandle.join()
183 hProgress2ThreadHandle.join()
186 ret = cam.MV_CC_CloseDevice()
188 raise Exception (
"close deivce fail! ret[0x%x]" % ret)
192 ret = cam.MV_CC_DestroyHandle()
194 except Exception
as e:
196 cam.MV_CC_CloseDevice()
197 cam.MV_CC_DestroyHandle()
200 MvCamera.MV_CC_Finalize()